﻿//创建时间: 2010-10-29
//创建作者: 李晓忠
//功能说明: CMS系统 页面发布 任务分解
//修改: 2010-11-22 李城 完成代码的编写 
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.CMS.Web.UI;
using TRPOP.CMS.Model;
using TRPOP.CMS.Business;
using TRPOP.Config;
using Newtonsoft.Json;
using TRPOP.Common;


namespace TRPOP.CMS.Web.Admin
{
    public partial class admin_taskanalysis : BasePage
    {
        #region 页面变量
        private ContentManage m_ContentManage = new ContentManage();
        private NodeManage m_NodeManage = new NodeManage();
        private string m_strAction = "";
        #endregion

        #region 页面事件

        protected void Page_Load(object sender, EventArgs e)
        {
            m_strAction = RequestHelper.RequestString("ac");
            if (m_strAction.IndexOf("ajax") > -1)
            {
                AjaxMethod();
            }
            else
            {
                if (!IsPostBack)
                {
                    LoadBase();
                }
            }
        }


        #endregion

        #region 页面方法

        /// <summary>
        /// 操作导航
        /// </summary>
        /// <summary>
        public void LoadBase()
        {
            string strNodeids = TRPOP.Common.RequestHelper.RequestString("nodeid").Trim();//操作的结点
            string strContentids = TRPOP.Common.RequestHelper.RequestString("contentid").Trim();//所有操作的内容ids
            string strAcType = TRPOP.Common.RequestHelper.RequestString("ac").Trim();//操作类型
            int intOnceOperTemp = TRPOP.Common.RequestHelper.RequestString("onceOper").Trim().ToInt();
            if (strAcType.IsEmpty() == false)
            {
                #region
                bool blnNode = false;
                bool blnContent = false;
                bool blnExtra = false;
                bool include_subBln = (TRPOP.Common.RequestHelper.RequestString("include_sub").Trim() == "1");
                int intOnceOper = intOnceOperTemp >= 2 ? 2 : 1;
                PubStatus.EnumElement oPubStatus = PubStatus.EnumElement.Publish;
                string extrs = GetExtraids(TRPOP.Common.RequestHelper.RequestString("nodeid").Trim());
                switch (strAcType.Trim().ToLower())
                {
                    #region 根据传递的参数导航到不同的函数进行执行
                    // 新增的发布 更新 信息ids
                    //
                    case "repubcontent"://只重新（更新）发布内容
                        blnContent = true;
                        GetContentids(ref  strContentids, strNodeids, PubStatus.EnumElement.Publish);
                        break;
                    case "pubcontent"://只发布内容
                        blnContent = true;
                        GetContentids(ref  strContentids, strNodeids, PubStatus.EnumElement.UnPublish);
                        break;
                    case "pubnode"://只发布结点首页
                        blnNode = true;
                        break;
                    case "puball"://发布内容、结点首页
                        blnNode = true;
                        blnContent = true;
                        break;
                    case "repubnode"://只重新（更新）发布结点首页
                        blnNode = true;
                        break;
                    case "repuball"://重新（更新）发布内容、结点首页]
                        blnNode = true;
                        blnContent = true;
                        oPubStatus = PubStatus.EnumElement.Publish;
                        break;
                    case "noderefresh"://站点更新
                        blnNode = (TRPOP.Common.RequestHelper.RequestString("refresh_index").Trim() == "1");
                        blnContent = (TRPOP.Common.RequestHelper.RequestString("refresh_content").Trim() == "1");
                        oPubStatus = PubStatus.EnumElement.Publish;
                        blnExtra = (TRPOP.Common.RequestHelper.RequestString("refresh_extra").Trim() == "1");
                        int intonceOperTemp = 0;
                        int.TryParse(TRPOP.Common.RequestHelper.RequestString("onceOper").Trim(), out intonceOperTemp);
                        if (intonceOperTemp > 0)
                            intOnceOper = intonceOperTemp;
                        break;
                    case "nodepub"://结点发布
                        blnNode = true;
                        blnContent = true;
                        oPubStatus = PubStatus.EnumElement.UnPublish;
                        break;
                    case "allnodepub"://整站发布
                        strNodeids = "0";
                        oPubStatus = PubStatus.EnumElement.UnPublish;
                        blnContent = true;
                        include_subBln = true;
                        break;
                    case "allnoderefresh"://整站更新
                        strNodeids = "0";
                        blnNode = (TRPOP.Common.RequestHelper.RequestString("refresh_index").Trim() == "1");
                        blnContent = (TRPOP.Common.RequestHelper.RequestString("refresh_content").Trim() == "1");
                        oPubStatus = PubStatus.EnumElement.Publish;
                        blnExtra = (TRPOP.Common.RequestHelper.RequestString("refresh_extra").Trim() == "1");
                        include_subBln = true;
                        break;
                    default:
                        break;
                    #endregion
                }
                JavaScriptObject otasks = new JavaScriptObject();
                otasks.Add("action", strAcType);
                JavaScriptObject oitemlist = new JavaScriptObject();
                #region 组合 oitemlist
                int indexbln = blnNode ? 1 : 0;
                int extrabln = blnExtra ? 1 : 0;
                int contentbln = blnContent ? 1 : 0;
                int intnodeid = 0;
                int.TryParse(strNodeids, out intnodeid);
                NodeInfo oNodeInfo = m_NodeManage.GetInfoById(intnodeid);
                if (oNodeInfo != null)
                {
                    oitemlist.Add(oNodeInfo.NodeId.ToString(), GetOneJSobject(oNodeInfo, blnExtra, blnContent, oPubStatus, strContentids));
                }
                #region 包含子节点的处理
                if (include_subBln)//如果多个结点
                {
                    List<NodeInfoWithLevel> list = new NodeManage().GetNodeListTree(intnodeid, 0, "");
                    foreach (NodeInfoWithLevel oinfo in list)
                    {
                        oitemlist.Add(oinfo.NodeId.ToString(), GetOneJSobject(oinfo as NodeInfo, blnExtra, blnContent, oPubStatus, strContentids));
                    }
                }
                #endregion
                #endregion
                otasks.Add("items", oitemlist);
                otasks.Add("extrabln", extrabln);//附加发布
                otasks.Add("indexbln", indexbln);//首页
                otasks.Add("contentbln", contentbln);
                otasks.Add("listpagecount", SYSConfig.SystemVar_listPageCount);
                otasks.Add("onceoper", intOnceOper);
                otasks.Add("result", "1");
                otasks.Add("ac", strAcType);
                otasks.Add("bind", "0");
                // Response.Write("<script>" + "var taskData=" + JavaScriptConvert.SerializeObject(otasks) + ";" + "</script>");
                Response.Write(JavaScriptConvert.SerializeObject(otasks));
                base.End();
                #endregion
            }
        }




        /// <summary>
        /// contentid的二次处理，是否反查结点下的所有信息
        /// </summary>
        /// <param name="strNodeids"></param>
        /// <param name="EnumPublish"></param>
        /// <returns></returns>
        public string GetContentIds(string strNodeids, PubStatus.EnumElement EnumPublish)
        {
            bool nodeFlash = (strNodeids.Trim().IsEmpty() == false);//如果传递结点，则查找结点下的内容
            string strContentids = "";
            if (nodeFlash)//如果结点更新、发布 
            {
                string strWhere = string.Format("and  NodeId in  ({0}) and PubStatus={1}", strNodeids, (int)EnumPublish);
                List<ContentInfo> olist = (new ContentManage()).GetListByCondition(300, strWhere, "  PubStatus desc,TopWeight desc,PubTime desc,DisplayOrder desc ");
                strContentids = "";
                foreach (ContentInfo oInfo in olist)
                {
                    strContentids += oInfo.ContentId.ToString() + ",";
                }
                if (strContentids.Length >= 1)
                    strContentids = strContentids.Remove(strContentids.Length - 1);
            }
            //虚链接Id
            if (strContentids != "")
            {
                string strVirtualIds = "";
                List<ContentInfo> vlist = m_ContentManage.GetVirtualLink(strContentids);
                foreach (ContentInfo oInfo in vlist)
                {
                    strVirtualIds += oInfo.ContentId.ToString() + ",";
                }
                if (strVirtualIds.Length >= 1)
                    strVirtualIds = strVirtualIds.Remove(strVirtualIds.Length - 1);
                if (strVirtualIds != "")
                {
                    strContentids = strContentids + "," + strVirtualIds;
                }
            }
            return strContentids;
        }

        public void GetContentids(ref string strContentids, string strNodeids, PubStatus.EnumElement EnumPublish)
        {
            if (strContentids.IsEmpty() || strNodeids.IsEmpty())
                return;
            string strWhere = string.Format("and    contentid in  ({0}) and PubStatus={1} and  nodeid in ({2}) ", strContentids, (int)EnumPublish, strNodeids);
            List<ContentInfo> olist = (new ContentManage()).GetListByCondition(0, strWhere, "  PubStatus desc,TopWeight desc,PubTime desc,DisplayOrder desc ");
            strContentids = string.Empty;
            foreach (ContentInfo oInfo in olist)
            {
                strContentids += oInfo.ContentId.ToString() + ",";
            }
            if (strContentids.Length >= 1)
                strContentids = strContentids.Remove(strContentids.Length - 1);
            else
                return;

            #region 虚链接Id
            if (strContentids != "")
            {
                string strVirtualIds = "";
                List<ContentInfo> vlist = m_ContentManage.GetVirtualLink(strContentids);
                foreach (ContentInfo oInfo in vlist)
                {
                    strVirtualIds += oInfo.ContentId.ToString() + ",";
                }
                if (strVirtualIds.Length >= 1)
                    strVirtualIds = strVirtualIds.Remove(strVirtualIds.Length - 1);
                if (strVirtualIds != "")
                {
                    strContentids = strContentids + "," + strVirtualIds;
                }
            }
            #endregion

        }

        /// <summary>
        /// 创建单个结点操作信息
        /// </summary>
        /// <param name="oNodeInfo"></param>
        /// <param name="indexbln"></param>
        /// <param name="contentbln"></param>
        /// <param name="blnExtra"></param>
        /// <param name="blnContent"></param>
        /// <param name="oPubStatus"></param>
        /// <returns></returns>
        public JavaScriptObject GetOneJSobject(NodeInfo oNodeInfo, bool blnExtra, bool blnContent, PubStatus.EnumElement oPubStatus, string strCntentids)
        {
            JavaScriptObject objItem = new JavaScriptObject();
            objItem.Add("nodeid", oNodeInfo.NodeId);
            objItem.Add("nodename", oNodeInfo.Name);
            objItem.Add("indexs", oNodeInfo.NodeId);//暂时未使用
            objItem.Add("extras", (blnExtra ? GetExtraids(oNodeInfo.NodeId.ToString()) : ""));
            if (strCntentids.IsEmpty())
            {
                objItem.Add("contents", (blnContent ? GetContentIds(oNodeInfo.NodeId.ToString(), oPubStatus) : ""));
            }
            else
            {
                objItem.Add("contents", strCntentids);
            }
            return objItem;
        }

        /// <summary>
        /// 根据结点找到所有的附加发布
        /// </summary>
        /// <param name="strNodeid"></param>
        /// <returns></returns>
        public string GetExtraids(string strNodeid)
        {
            string strReturn = string.Empty;
            int intNodeid = 0;
            int.TryParse(strNodeid, out intNodeid);
            if (intNodeid > 0)
            {
                NodeAddionalPubManage oNodeAddionalPubManage = new NodeAddionalPubManage();
                List<NodeAddionalPubInfo> olist = oNodeAddionalPubManage.GetListByCondition(0, " and  NodeId=" + intNodeid.ToString() + " ", "");
                foreach (NodeAddionalPubInfo oTemp in olist)
                {
                    strReturn += "," + oTemp.NodeAddionalPubId.ToString();
                }
                if (strReturn.IsEmpty() == false)
                {
                    strReturn = strReturn.Substring(1);
                }
            }
            return strReturn;
        }
        /// <summary>
        /// 处理Ajax请求
        /// </summary>
        private void AjaxMethod()
        {
            JavaScriptObject result = new JavaScriptObject();
            JavaScriptObject items = new JavaScriptObject();
            JavaScriptObject item = new JavaScriptObject();
            m_strAction = m_strAction.ToLower();

            switch (m_strAction)
            {
                case "ajax_noderefresh":
                    #region 结点更新
                    #region 参数
                    int m_nodeid = RequestHelper.RequestInt("nodeid");
                    int m_refresh_index = RequestHelper.RequestInt("refresh_index");
                    int m_refresh_content = RequestHelper.RequestInt("refresh_content");
                    int m_refresh_extra = RequestHelper.RequestInt("refresh_extra");
                    int m_include_sub = RequestHelper.RequestInt("include_sub");
                    int m_onceOper = RequestHelper.RequestInt("onceOper");
                    if (m_onceOper <= 0)
                        m_onceOper = 10;
                    if (m_onceOper > 100)
                        m_onceOper = 100;
                    #endregion
                    if (m_nodeid <= 0)//整站更新
                    {
                        #region 整站更新
                        foreach (NodeInfo mNode in m_NodeManage.GetListByCondition(0, " And IsHidde=0 And ContentPubTypeId<>" + ContentPubType.EnumElement.NotOutPub.GetHashCode(), ""))
                        {
                            item = new JavaScriptObject();
                            item.Add("nodeid", mNode.NodeId);
                            item.Add("ac", "refresh");
                            item.Add("name", mNode.Name);
                            item.Add("refresh_index", m_refresh_index);
                            item.Add("refresh_content", m_refresh_content);
                            item.Add("refresh_extra", m_refresh_extra);
                            item.Add("onceOper", m_onceOper);
                            item.Add("htmlpagecount", SYSConfig.SystemVar_listPageCount);
                            item.Add("exec", "0");
                            if (!items.ContainsKey(mNode.NodeId.ToString()))
                                items.Add(mNode.NodeId.ToString(), item);

                        }
                        result.Add("des", "整站更新");
                        result.Add("items", items);
                        #endregion
                    }
                    else //结点更新
                    {
                        #region 结点更新
                        List<NodeInfo> m_nodeList;
                        if (m_include_sub == 1)
                        {
                            m_nodeList = m_NodeManage.GetNodeListByParentID(m_nodeid);
                        }
                        else
                        {
                            m_nodeList = m_NodeManage.GetListByCondition(1, " And  NodeId=" + m_nodeid + " And ContentPubTypeId<>" + ContentPubType.EnumElement.NotOutPub.GetHashCode(), "");
                        }
                        foreach (NodeInfo mNode in m_nodeList)
                        {
                            if (mNode.ContentPubTypeId == ContentPubType.EnumElement.NotOutPub.GetHashCode())
                            {
                                continue;
                            }
                            item = new JavaScriptObject();
                            item.Add("nodeid", mNode.NodeId);
                            item.Add("ac", "refresh");
                            item.Add("name", mNode.Name);
                            item.Add("refresh_index", m_refresh_index);
                            item.Add("refresh_content", m_refresh_content);
                            item.Add("refresh_extra", m_refresh_extra);
                            item.Add("onceOper", m_onceOper);
                            item.Add("htmlpagecount", SYSConfig.SystemVar_listPageCount);
                            item.Add("exec", "0");
                            if (!items.ContainsKey(mNode.NodeId.ToString()))
                                items.Add(mNode.NodeId.ToString(), item);

                        }
                        result.Add("des", "结点批量更新");
                        result.Add("items", items);
                        #endregion
                    }
                    result.Add("bind", "0");
                    result.Add("result", "1");
                    result.Add("msg", "ok");
                    #endregion
                    break;
                case "ajax_contentrefresh":
                    #region 分页获取要更新的ContentIds
                    #region 参数
                    m_nodeid = RequestHelper.RequestInt("nodeid");
                    int m_pageSize = RequestHelper.RequestInt("pageSize");
                    int m_currtentPage = RequestHelper.RequestInt("currtentPage");
                    #endregion
                    int m_recordCount = 0;
                    string m_strIds = string.Empty;
                    foreach (ContentInfo info in m_ContentManage.GetContetnIDListByPager(m_currtentPage, m_pageSize, " and Nodeid=" + m_nodeid + " and  PubStatus=" + PubStatus.EnumElement.Publish.GetHashCode(), BaseAdmin.m_ContentDisplayOrderBY, out m_recordCount))
                    {
                        m_strIds += "," + info.ContentId.ToString();
                    }
                    if (m_strIds.IsEmpty() == false)
                    {
                        m_strIds = m_strIds.Substring(1);
                    }
                    result.Add("ids", m_strIds);
                    result.Add("totalPage", (m_recordCount + m_pageSize) / m_pageSize);
                    result.Add("bind", "0");
                    result.Add("result", "1");
                    result.Add("msg", "ok");
                    #endregion
                    break;
                case "ajax_extrarefresh":
                    #region 获取要更新的附加发布ids

                    #region 参数
                    m_nodeid = RequestHelper.RequestInt("nodeid");
                    #endregion

                    m_strIds = string.Empty;
                    NodeAddionalPubManage m_NodeAddionalPubManage = new NodeAddionalPubManage();
                    foreach (NodeAddionalPubInfo info in m_NodeAddionalPubManage.GetListByCondition(0, " And Nodeid=" + m_nodeid, ""))
                    {
                        m_strIds += "," + info.NodeAddionalPubId.ToString();
                    }
                    if (m_strIds.IsEmpty() == false)
                    {
                        m_strIds = m_strIds.Substring(1);
                    }
                    result.Add("ids", m_strIds);
                    result.Add("result", "1");
                    result.Add("msg", "ok");
                    #endregion
                    break;
                case "ajax_nodepub":
                    #region 结点发布
                    #region 参数
                    m_nodeid = RequestHelper.RequestInt("nodeid");
                    m_include_sub = RequestHelper.RequestInt("include_sub");
                    m_onceOper = RequestHelper.RequestInt("onceOper");
                    if (m_onceOper <= 0)
                        m_onceOper = 10;
                    if (m_onceOper > 100)
                        m_onceOper = 100;
                    #endregion
                    if (m_nodeid <= 0)//整站发布
                    {
                        #region 整站发布
                        foreach (NodeInfo mNode in m_NodeManage.GetListByCondition(0, " And IsHidde=0 And ContentPubTypeId<>" + ContentPubType.EnumElement.NotOutPub.GetHashCode(), ""))
                        {

                            item = new JavaScriptObject();
                            item.Add("nodeid", mNode.NodeId);
                            item.Add("ac", "nodepub");
                            item.Add("name", mNode.Name);
                            item.Add("pub_index", 1);
                            item.Add("pub_content", 1);
                            item.Add("onceOper", m_onceOper);
                            item.Add("htmlpagecount", SYSConfig.SystemVar_listPageCount);
                            item.Add("exec", "0");
                            if (!items.ContainsKey(mNode.NodeId.ToString()))
                                items.Add(mNode.NodeId.ToString(), item);

                        }
                        result.Add("des", "整站发布");
                        result.Add("items", items);
                        #endregion
                    }
                    else //结点发布
                    {
                        #region 结点发布
                        List<NodeInfo> m_nodeList;
                        if (m_include_sub == 1)
                        {
                            m_nodeList = m_NodeManage.GetNodeListByParentID(m_nodeid);
                        }
                        else
                        {
                            m_nodeList = m_NodeManage.GetListByCondition(1, " And  NodeId=" + m_nodeid, "");
                        }
                        foreach (NodeInfo mNode in m_nodeList)
                        {
                            if (mNode.ContentPubTypeId == ContentPubType.EnumElement.NotOutPub.GetHashCode())
                            {
                                continue;
                            }
                            item = new JavaScriptObject();
                            item.Add("nodeid", mNode.NodeId);
                            item.Add("ac", "nodepub");
                            item.Add("name", mNode.Name);
                            item.Add("pub_index", 1);
                            item.Add("pub_content", 1);
                            item.Add("onceOper", m_onceOper);
                            item.Add("htmlpagecount", SYSConfig.SystemVar_listPageCount);
                            item.Add("exec", "0");
                            items.Add(mNode.NodeId.ToString(), item);
                            if (!items.ContainsKey(mNode.NodeId.ToString()))
                                items.Add(mNode.NodeId.ToString(), item);

                        }
                        result.Add("des", "结点批量发布");
                        result.Add("items", items);
                        #endregion
                    }
                    result.Add("bind", "0");
                    result.Add("result", "1");
                    result.Add("msg", "ok");
                    #endregion
                    break;
                case "ajax_contentpub":
                    #region 分页获取内容发布Ids
                    #region 参数
                    m_nodeid = RequestHelper.RequestInt("nodeid");
                    m_pageSize = RequestHelper.RequestInt("pageSize");
                    m_currtentPage = RequestHelper.RequestInt("currtentPage");
                    #endregion
                    m_recordCount = 0;
                    m_strIds = string.Empty;
                    foreach (ContentInfo info in m_ContentManage.GetContetnIDListByPager(m_currtentPage, m_pageSize, " and Nodeid=" + m_nodeid + " and  PubStatus=" + PubStatus.EnumElement.UnPublish.GetHashCode(), BaseAdmin.m_ContentDisplayOrderBY, out m_recordCount))
                    {
                        m_strIds += "," + info.ContentId.ToString();
                    }
                    if (m_strIds.IsEmpty() == false)
                    {
                        m_strIds = m_strIds.Substring(1);
                    }
                    result.Add("ids", m_strIds);
                    result.Add("totalPage", (m_recordCount + m_pageSize) / m_pageSize);
                    result.Add("bind", "0");
                    result.Add("result", "1");
                    result.Add("msg", "ok");
                    #endregion
                    break;
                default:
                    result.Add("result", "1");
                    result.Add("msg", "ok");
                    break;
            }
            Response.Write(JavaScriptConvert.SerializeObject(result));
            base.End();
        }
        #endregion
    }
}
